package com.team5101.lha.team5101.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.team5101.base.BaseServiceImpl;
import com.team5101.base.IBaseDao;
import com.team5101.dto.AllOrderDto;
import com.team5101.dto.OrderDetailDto;
import com.team5101.dto.OrderDto;
import com.team5101.entity.TOrder;
import com.team5101.entity.TOrderDetail;
import com.team5101.entity.TUser;
import com.team5101.mapper.TOrderDetailMapper;
import com.team5101.mapper.TOrderMapper;
import com.team5101.shop.api.IOrderService;
import com.team5101.utils.CreateUtil;
import com.team5101.vo.AllOrderVo;
import com.team5101.vo.OrderVo;
import com.team5101.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class OrderServiceImpl extends BaseServiceImpl<TOrder> implements IOrderService{

    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private TOrderDetailMapper tOrderDetailMapper;

    @Override
    public IBaseDao<TOrder> getBaseDao() {
        return tOrderMapper;
    }

    public int insertBatchOrderDetail(List<TOrderDetail> orderDetails){
        return tOrderDetailMapper.insertBatchOrderDetail(orderDetails);
    }


    @Override
    public ResultBean createOrder(OrderDto orderDto) {
        TUser tUser = orderDto.gettUser();
        TOrder tOrder = new TOrder();
        tOrder.setoReceive(tUser.getuName());
        tOrder.setoAddr(tUser.getuAddr());
        tOrder.setoPhone(tUser.getuPhone());
        tOrder.setoMark(orderDto.getoMark());
        tOrder.setoUid(tUser.getuId());
        tOrder.setoPsum(orderDto.getoPsum());
        tOrder.setoStatus(0);

        long now = System.currentTimeMillis();
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        Date date = new Date(now);
        long later = (now +30*1000*60);
        String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(later);
        tOrder.setoCreattime(createTime);
        tOrder.setoEndtime(endTime);
        String orderNumber = CreateUtil.createOrderNumber();
        tOrder.setoOrdernum(orderNumber);

        int i = tOrderMapper.insertSelective(tOrder);
        Long oid =tOrderMapper.getoId(orderNumber);
        for (TOrderDetail orderDetail : orderDto.getProductInfo()){
            orderDetail.setoCreattime(createTime);
            orderDetail.setcEndtime(endTime);
            orderDetail.setoOid(oid);
        }
        int Num = insertBatchOrderDetail(orderDto.getProductInfo());
        OrderVo orderVo = new OrderVo();
        orderVo.setoCreattime(createTime);
        orderVo.setoEndtime(endTime);
        orderVo.setoOrdernum(orderNumber);
        orderVo.setoStatus(0);
        if(Num>=1){
          return ResultBean.success(orderVo,"添加成功");
        }

        return ResultBean.error(null,"添加错误");
    }

    @Override
    public List<AllOrderDto> getAllOrder(Integer uId) {
        return tOrderMapper.getAllOrder(uId);
    }

    @Override
    public List<OrderDetailDto> queryOrderDetail(Integer oId) {
        return tOrderMapper.queryOrderDetail(oId);
    }
}
